//
//  DetailViewController.m
//  UI_豆瓣更新版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "DetailViewController.h"
#import "UIImageView+WebCache.h"
#import "CYDataBaseHandle.h"
@interface DetailViewController ()

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIAlertController *alert;
@property (nonatomic, retain) NSMutableArray *arr_;

@end

@implementation DetailViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg_nav@2x.png"] forBarMetrics:UIBarMetricsDefault];
    }
    return self;
}

- (void)dealloc {
    [_arr_ release];
    [_scrollView release];
    [_alert release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.str;
    
    // 详情页面布局
    [self DetailLayout];
    
}

// 返回按钮点击方法
- (void)itemAction:(UIBarButtonItem *)item {
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

// 收藏按钮点击方法
- (void)itemAction1:(UIBarButtonItem *)item {
    
    // 数组中包含的是保存到数据库中的 title
    NSArray *arr = [[CYDataBaseHandle sharedDataBaseHandle] selectTable];
    self.arr_ = [NSMutableArray array];
    for (ActivityModel *model in arr) {
        [_arr_ addObject:model.title];
    }
    
    // 如果数据库中的 title 与上一页传过来的detailModel.title 一致  则数据库中已经存在   提示已经收藏
    // 否则  将detailModel 整个数据添加到数据库中
    if ([_arr_ containsObject:_detailModel.title]) {
        self.alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已被收藏" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:_alert animated:YES completion:nil];
                // 在弹出对话框上添加按钮
                    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                // 添加方法
                    [_alert addAction:alertAction];
    } else {
        [[CYDataBaseHandle sharedDataBaseHandle] insertModel:_detailModel];
        self.alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            // 显示视图对话框
            [self presentViewController:_alert animated:YES completion:nil];
        
            [self performSelector:@selector(dismissAlert) withObject:nil afterDelay:1.5];
            }
}

- (void)dismissAlert {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}



- (void)DetailLayout {
    
    UIImage *img5 = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    img5 = [img5 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:img5 style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.leftBarButtonItem = item;
    
    UIImage *img6 = [UIImage imageNamed:@"btn_nav_share@2x.png"];
    img6 = [img6 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:img6 style:UIBarButtonItemStylePlain target:self action:@selector(itemAction1:)];
    self.navigationItem.rightBarButtonItem = item1;
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49)];
    // 描述文本大小
//    NSDictionary *textDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
//    CGRect rect = [_detailModel.content boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:textDic context:nil];
    
    _scrollView.contentSize = CGSizeMake(0, self.view.frame.size.height * 2);
    
    [self.view addSubview:_scrollView];
    [_scrollView release];
    
    // 活动名称Label
    UILabel *nameLahbel = [[UILabel alloc] initWithFrame:CGRectMake(20, 25, 335, 40)];
    nameLahbel.font = [UIFont systemFontOfSize:22];
    nameLahbel.text = _detailModel.title;
    [_scrollView addSubview:nameLahbel];
    
    // 活动图片
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 80, 110, 160)];
    NSString *str = _detailModel.image;
    [imgView sd_setImageWithURL:[NSURL URLWithString:str]];
    [_scrollView addSubview:imgView];
    
    // 时间图片
    UIImageView *img1 = [[UIImageView alloc] initWithFrame:CGRectMake(135, 80, 25, 25)];
    img1.image = [UIImage imageNamed:@"icon_date_blue@2x.png"];
    [_scrollView addSubview:img1];
    // 时间Label
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 77, 195, 30)];
    timeLabel.font = [UIFont systemFontOfSize:15];
    NSString *aStr = [_detailModel.begin_time substringWithRange:NSMakeRange(5, 11)];
    NSString *bStr = [_detailModel.end_time substringWithRange:NSMakeRange(5, 11)];
    NSString *cStr = [NSString stringWithFormat:@"%@ -- %@", aStr, bStr];
    timeLabel.text = cStr;
    [_scrollView addSubview:timeLabel];
    
    // 活动场馆图片
    UIImageView *img2 = [[UIImageView alloc] initWithFrame:CGRectMake(135, 112, 25, 25)];
    img2.image = [UIImage imageNamed:@"icon_sponsor_blue@2x.png"];
    [_scrollView addSubview:img2];
    // 活动场馆Label
    UILabel *museumLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 109, 195, 30)];
    museumLabel.font = [UIFont systemFontOfSize:15];
    id dic = _detailModel.owner;
    museumLabel.text = dic[@"name"];
    [_scrollView addSubview:museumLabel];
    [museumLabel release];
    
    // 类型图片
    UIImageView *img3 = [[UIImageView alloc] initWithFrame:CGRectMake(135, 144, 25, 25)];
    img3.image = [UIImage imageNamed:@"icon_catalog_blue@2x.png"];
    [_scrollView addSubview:img3];
    // 类型Label
    UILabel *Label = [[UILabel alloc] initWithFrame:CGRectMake(160, 140, 50, 30)];
    Label.font = [UIFont systemFontOfSize:15];
    Label.text = @"类型: ";
    [_scrollView addSubview:Label];
    UILabel *cLabel = [[UILabel alloc] initWithFrame:CGRectMake(210, 140, 60, 30)];
    cLabel.font = [UIFont systemFontOfSize:15];
    cLabel.text = _detailModel.category_name;
    [_scrollView addSubview:cLabel];
    
    // 活动地址图片
    UIImageView *img4 = [[UIImageView alloc] initWithFrame:CGRectMake(135, 176, 25, 25)];
    img4.image = [UIImage imageNamed:@"icon_spot_blue@2x.png"];
    [_scrollView addSubview:img4];
    // 活动地址Label
    UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 172, 195, 30)];
    addressLabel.font = [UIFont systemFontOfSize:15];
    addressLabel.text = _detailModel.address;
    addressLabel.numberOfLines = 2;
    [_scrollView addSubview:addressLabel];
    
    // "活动介绍"Label
    UILabel *bigLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 250, 150, 40)];
    bigLabel.text = @"活动介绍";
    bigLabel.font = [UIFont systemFontOfSize:22];
    [_scrollView addSubview:bigLabel];
    [bigLabel release];
    
    // 详情Label
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, self.view.frame.size.width - 40, 2 * self.view.frame.size.height- 355)];
    detailLabel.text = _detailModel.content;
    detailLabel.numberOfLines = 80;
    [_scrollView addSubview:detailLabel];
    [detailLabel release];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
